package org.geektimes.projects.user.web.controller;

import org.geektimes.di.context.ComponentContext;
import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

/**
 * 输出 “Hello,World” Controller
 */
@Path("/user")
public class LoginController implements PageController {

    private UserService userService;

    {
        this.userService = ComponentContext.getInstance().getComponent("bean/UserService");
    }

    @GET
    @POST
    @Path("/login")
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        User user = new User();
        user.setName(request.getParameter("name"));
        user.setPassword(request.getParameter("password"));

        boolean login = userService.login(user);
        if(login) {
            return "loginSuccess.jsp";
        }else{
            return "loginFail.jsp";
        }
    }
}
